<!DOCTYPE html>
<html lang="en" xmlns:v-bind="http://www.w3.org/1999/xhtml">
<head>
    <meta charset="UTF-8">
    <meta content="text/html;charset=UTF-8"/>
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta content="width=device-width, initial-scale=1, maximum-scale=1, user-scalable=no" name="viewport">

    <link rel="stylesheet" href="../lib/elementui/lib/theme-default/index.css"/>

    <!-- 引入组件库 -->
    <!-- 先引入 Vue -->
    <script src="../lib/jquery/jquery-3.2.1.min.js"></script>
    <script src="../lib/vue/vue.js"></script>
    <script src="../lib/vue/vue-resource.min.js"></script>
    <script src="../lib/vue/vue-router.js"></script>
    <script src="../lib/elementui/lib/index.js"></script>
    <title>Title</title>
</head>


<body>
<el-form id="form" :model="dynamicValidateForm" ref="dynamicValidateForm" label-width="100px" class="demo-dynamic">
    <el-form-item
            prop="email"
            label="邮箱"
            :rules="[
      { required: true, message: '请输入邮箱地址', trigger: 'blur' },
      { type: 'email', message: '请输入正确的邮箱地址', trigger: 'blur,change' }
    ]"
    >
        <el-input v-model="dynamicValidateForm.email"></el-input>
    </el-form-item>
    <el-form-item
            v-for="(domain, index) in dynamicValidateForm.domains"
            :label="'域名' + index"
            :prop="'domains.' + index + '.name'"
            :rules="{
      required: true, message: '域名不能为空', trigger: 'blur,change'
    }"
    >
        <el-input v-model="domain.name"></el-input>
        <el-button @click.prevent="removeDomain(domain)">删除</el-button>
    </el-form-item>
    <el-form-item>
        <el-button type="primary" @click="submitForm('dynamicValidateForm')">提交</el-button>
        <el-button @click="addDomain">新增域名</el-button>
        <el-button @click="resetForm('dynamicValidateForm')">重置</el-button>
    </el-form-item>
</el-form>

</body>


<script types="text/javascript">

    new Vue({
        el: '#form',
        data: function () {
            return {
                dynamicValidateForm: {
                    domains: [{
                    }],
                    email: ''
                }
            };
        },
        methods: {
            submitForm: function (formName) {
                this.$refs[formName].validate(
                    function (valid) {
                        if (valid) {
                            alert('submit!');
                        } else {
                            console.log('error submit!!');
                            return false;
                        }
                    });
            },
            resetForm: function (formName) {
                this.$refs[formName].resetFields();
            },
            removeDomain: function (item) {
                var index = this.dynamicValidateForm.domains.indexOf(item)
                if (index !== -1) {
                    this.dynamicValidateForm.domains.splice(index, 1)
                }
            },
            addDomain: function () {
                this.dynamicValidateForm.domains.push({
                    value: '',
                    key: Date.now()
                });
            }
        }
    })


</script>

</html>